/**
 * 
 */
package fr.cie.blog.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import fr.cie.blog.beans.Article;
import fr.cie.blog.domainservice.IServiceArticle;
import fr.cie.blog.domainservice.IServiceCategory;
import fr.cie.blog.services.paginator.Paginator;

/**
 * @author Jee3
 * 
 */
public class ArticleAction extends GenericAction implements
		ServletRequestAware {

	// =========================== properties =============================== //

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(ArticleAction.class);

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 2080032605123310503L;

	/**
	 * request.
	 */
	private HttpServletRequest request;

	/**
	 * article.
	 */
	private Article article;
	/**
	 * articleList.
	 */
	private List<Article> articleList;

	/**
	 * id.
	 */
	private int id;

	/**
	 * paginator.
	 */
	private Paginator paginator;

	/**
	 * string.
	 */
	private String string;

	// ============================ Services =================================//

	/**
	 * serviceArticle.
	 */
	private final IServiceArticle serviceArticle;
	/**
	 * serviceCategory.
	 */
	private final IServiceCategory serviceCategory;

	// ===================== Constructor =============================//

	/**
	 * Constructor.
	 * 
	 * @param pServiceArticle
	 *            serviceArticle
	 * @param pServiceCategory
	 *            serviceCategory
	 */
	public ArticleAction(final IServiceArticle pServiceArticle,
			final IServiceCategory pServiceCategory) {
		super();
		serviceArticle = pServiceArticle;
		serviceCategory = pServiceCategory;
	}

	// ===================== getters and setters =============================//

	/**
	 * @return the article
	 */
	public final Article getArticle() {
		return article;
	}

	/**
	 * @return the articleList
	 */
	public final List<Article> getArticleList() {
		return articleList;
	}

	/**
	 * @return the id
	 */
	public final int getId() {
		return id;
	}

	/**
	 * @return the paginator
	 */
	public final Paginator getPaginator() {
		return paginator;
	}

	/**
	 * @return the string
	 */
	public final String getString() {
		return string;
	}

	/**
	 * @param pArticle
	 *            the article to set
	 */
	public final void setArticle(final Article pArticle) {
		article = pArticle;
	}

	/**
	 * @param pArticleList
	 *            the articleList to set
	 */
	public final void setArticleList(final List<Article> pArticleList) {
		articleList = pArticleList;
	}

	// ====================== ACTIONS METHODS WITH RESULT==================== //
	// ========= simple CRUD ===== //

	/**
	 * @param pId
	 *            the id to set
	 */
	public final void setId(final int pId) {
		id = pId;
	}

	// ====================== ACTIONS METHODS WITH RESULT==================== //
	// ========= simple CRUD ===== //

	/**
	 * @param pPaginator
	 *            the paginator to set
	 */
	public final void setPaginator(final Paginator pPaginator) {
		paginator = pPaginator;
	}

	/**
	 * @param pString
	 *            the string to set
	 */
	public final void setString(final String pString) {
		this.string = pString;
	}

	// ======= IMPLEMENTED METHODS (Injection of dependency by Struts) ====== //

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(
	 * javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public final void setServletRequest(final HttpServletRequest pRequest) {
		this.request = pRequest;

	}

	// ====================== ACTIONS METHODS WITH RESULT==================== //
	// ========= simple CRUD ===== //
	/**
	 * @return SUCCESS
	 */
	public final String createArticle() {
		serviceArticle.save(article);
		return "";
	}

	/**
	 * @return SUCCESS
	 */
	public final String deleteArticle() {
		serviceArticle.delete(article);
		return "";
	}

	/**
	 * @return SUCCESS
	 */
	public final String modifiyArticle() {
		serviceArticle.update(article);
		return "";
	}

	/**
	 * @return SUCCESS
	 */
	public final String listArticlesByCategory() {
		LOG.info("Entering in listArticlesByCategory()" + id);

		String message;

		string = serviceCategory.findById(id).getName();

		final List<Article> articleListBase = serviceArticle
				.findByCategory(id);

		if (articleListBase == null) {
			message = ERROR;
		} else {
			articleList = articleListBase;
			message = SUCCESS;
		}

		return message;
	}

	/**
	 * @return SUCCESS
	 */
	public final String showAllArticles() {
		LOG.info("Entering in showAllArticles()");

		String message;
		final List<Article> articleListBase = serviceArticle.findAll();

		if (articleListBase == null) {
			message = ERROR;
		} else {
			articleList = articleListBase;
			message = SUCCESS;
		}

		return message;
	}

	/**
	 * Method to show the Article with its discussion.
	 * 
	 * @return SUCCESS or ERROR
	 */
	public final String showArticleWithComments() {
		LOG.info("Entering in showArticleWithComments(" + id + ")");

		String message;
		final Article articleBase = serviceArticle.findById(id);

		if (articleBase == null) {
			message = ERROR;
		} else {
			article = articleBase;
			message = SUCCESS;
		}

		return message;

	}

	// ======================= ad hoc french CRUD ..======================== //

	/**
	 * Method to get the five last published articles.
	 * 
	 * @return articleList.
	 * 
	 */
	public final List<Article> showFivelastArticles() {
		paginator = new Paginator();

		articleList = serviceArticle.findFiveLastArticles();

		LOG.info("Exit findFiveLastArticles() with size list : "
				+ articleList.size() + ", and 1st element : "
				+ articleList.get(0).getName() + "in category : "
				+ articleList.get(0).getCategory().getName());

		return articleList;
	}

	/**
	 * @return SUCCESS
	 */
	public final String updateXMLArticle() {
		return "";
	}

}
